using UnityEngine;
using UnityEngine.UI;

public class ItemModify : MonoBehaviour
{
    public enum ModifyType
    {
        Hp,Damage,Armor,Turn,Artifact,Candy,Ball,Flower
    }
    [Header("总名称")]//用来标识保存的
    public string myName = "";
    [Header("价值")]
    public int value = 1;
    [Header("类型")]
    public ModifyType type;
    [Header("符咒")]
    public string turnName = "鼠";

    [Header("是三选一吗")]
    public bool isChoose = false;

    //金币
    public GameObject coinObj;

    private void Awake()
    {
        ////找到金币
        //coinObj=transform.Find("Coin").gameObject;

        if(coinObj!=null)
        {
            coinObj.SetActive(false);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (coinObj!=null&&coinObj.activeSelf) return;
        if (!GameManager.Instance.canOprate) return;
        RunOut();
    }

    //实现效果
    public void RunOut()
    {
        if (type==ModifyType.Hp)
        {
            GameManager.Instance.ChangePlayerHp(value);
            TipSayManager.Instance.Say("生命值 <color=yellow> +"+value+" </color>");
        }
        else if (type==ModifyType.Damage)
        {
            GameManager.Instance.ChangeDamage(value);
            TipSayManager.Instance.Say("攻击力 <color=yellow> +"+value+" </color>");
        }
        else if (type==ModifyType.Armor)
        {
            GameManager.Instance.ChangeArmor(value);
            TipSayManager.Instance.Say("防御 <color=yellow> +"+value+" </color>");
        }
        else if (type==ModifyType.Turn)
        {
            TurnManager.instance.AddTurn(turnName);
            TipSayManager.Instance.Say(turnName+" 符咒 <color=yellow> +1 </color>");
        }
        else if(type==ModifyType.Artifact)
        {
            ArtifactManager.instance.AddArtifact(myName);
        }
        else if(type==ModifyType.Candy)
        {
            //随机选择攻击防御生命，攻击5-30，防御5-30，生命50-500提升
            int randomX = Random.Range(0, 90);
            if(randomX<30)
            {
                int ranV = Random.Range(5, 30);

                GameManager.Instance.ChangeDamage(ranV);
                TipSayManager.Instance.Say("你的<color=yellow> 幸运糖果 </color> 是 + 攻击力 <color=yellow> +"+ranV+" </color>");
            }
            else if(randomX<60)
            {
                int ranV = Random.Range(5, 30);

                GameManager.Instance.ChangeArmor(ranV);
                TipSayManager.Instance.Say("你的<color=yellow> 幸运糖果 </color> 是 + 防御力 <color=yellow> +"+ranV+" </color>");
            }
            else
            {
                int ranV = Random.Range(50, 500);

                GameManager.Instance.ChangePlayerHp(ranV);
                TipSayManager.Instance.Say("你的<color=yellow> 幸运糖果 </color> 是 + 生命力 <color=yellow> +"+ranV+" </color>");
            }
        }
        else if(type==ModifyType.Ball)
        {
            GameManager.Instance.ChangeBall(1);

            TipSayManager.Instance.Say("篮球<color=yellow> +1 </color>");
        }
        else if (type == ModifyType.Flower)
        {
            GameManager.Instance.ChangeFlower(1);

            TipSayManager.Instance.Say("希望之花<color=yellow> +1 </color>");
        }

        //移除权重
        if (isChoose == false)
        {
            SceneItemManager.instance.ReItemList(this.gameObject);
        }

        Destroy(gameObject);

        AudManager.instance.PlaySceneAud(AudManager.instance.sceneDic["金币"], 0.5f);

        //如果是三选一
        if (isChoose)
        {
            transform.parent.GetComponent<RandomRoomManager>().CloseAllItem();
        }
    }
    //随机房间设置
    public void SetRandomRoom(int _coin)
    {
        //找到金币
        coinObj.SetActive(true);
        coinObj.transform.Find("Text").GetComponent<Text>().text=_coin.ToString();
    }
}
